<template>
    <div class="h-100%">
        <el-page-header @back="gotoRoute('/eva-project/pager')" content="测评主体设置">
        </el-page-header>
        <el-divider></el-divider>
        <el-alert :title="'该企业' + (group_type == 0 ? '无' : '有') + '董事会,建议选择下列主体:'" type="warning" :closable="false">
            <span v-if="group_type == 1">A1,A2,B,C,D,E</span>
            <span v-if="group_type == 0">A11,B,C,D</span>
        </el-alert>
        <el-divider></el-divider>
        <el-button-group v-if="allowed([0])">
            <el-button :disabled="is_finished" size="mini" type="primary" @click="handEdit">新增</el-button>
        </el-button-group>
        <el-divider></el-divider>
        <el-table :data="tableData.rows" border class="w-90%">
            <el-table-column prop="owner_name_name" label="主体名称" width="100">
            </el-table-column>
            <el-table-column prop="plan_quantity" label="计划数量" width="100">
            </el-table-column>
            <el-table-column prop="remark" label="备注" width="220">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="400">
                <template slot-scope="scope">
                    <el-button-group>
                        <el-button :disabled="is_finished" v-if="allowed([0])" @click="handEdit(scope.row)"
                            type="success" size="small">编辑</el-button>
                        <el-button :disabled="is_finished" v-if="allowed([0])" @click="handForm(scope.row)"
                            type="primary" size="small">评测表设置</el-button>
                        <el-button :disabled="is_finished" @click="handFormUser(scope.row)" type="warning"
                            size="small">评测表主体人员设置</el-button>
                        <el-button :disabled="is_finished" v-if="allowed([0])" @click="handRemove(scope.row)"
                            type="danger" size="small">删除</el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background @current-change="handPage" layout="prev, pager, next" :total="tableData.count"
            :page-size="tableData.offset">
        </el-pagination>
    </div>
</template>

<script>
import { FormDialog } from '@formily/element'
import { InputForm } from './InputForm';
import { BaseApi } from '../../../api/common/base-api';
import { sharedConfig } from '../../shared/BaseCurd';
import { onFieldValueChange } from '@formily/core';
import { onFieldInfo } from '../../../api/common/field-event';
import { OwnerForm } from "./OwnerForm";
import { split6Value } from '../../../api/common/field-event';
const api = new BaseApi({ prefix: "/eva-project-owner" });
const owner_form_api = new BaseApi({ prefix: "/eva-project-owner-form" });

const entity = {
    id: undefined,
    project_id: "",
    owner_name_id: "",
    owner_name_name: "",
    plan_quantity: 0,
    remark: "",
}
const ownerFormEntity = {

    ru_form_info: [],
}
export default {
    name: "EvaProjectOwnerPager",
    props: ["project_id", "group_type", "is_finished"],
    async mounted() {
        this.loadPager({ page: 1 });
    },
    methods: {
        ...sharedConfig.methods,
        async loadPager({ page }) {
            const data = await api.findPager({ page, where: { project_id: this.project_id }, order: [["owner_name_name"], ["sort"]] });
            this.tableData = { ...data.data };
        },
        handEdit(row) {
            FormDialog('编辑评测主体名称', InputForm)
                .forOpen((payload, next) => {
                    const raw = (row instanceof PointerEvent) ? entity : row;
                    raw.project_id = this.project_id;
                    raw.group_type = this.group_type;
                    next({
                        initialValues: { ...raw },
                        effects() {
                            onFieldValueChange("owner_name_info", (field, form) => {
                                const names = ['owner_name_id', 'owner_name_name']
                                onFieldInfo({ names, field, form });
                            })
                        }
                    })
                })
                .forConfirm((payload, next) => {
                    next(payload)
                })
                .forCancel((payload, next) => {
                    next(payload)
                })
                .open()
                .then(async (values) => {
                    Reflect.deleteProperty(values, "owner_name_info");
                    await this._save({ api, values })
                    await this.loadPager({ page: 1 });
                })
                .catch(console.error)
        },
        handForm(row) {
            FormDialog('评测主体可评表设置', OwnerForm)
                .forOpen(async (payload, next) => {
                    const raw = { ...ownerFormEntity };
                    raw.project_id = row.project_id;
                    raw.project_owner_id = row.id;
                    const result = await owner_form_api.findList({ where: { project_id: row.project_id, project_owner_id: row.id } });
                    raw.ru_form_info = result.data.map(({ ru_form_info }) => ru_form_info);

                    next({
                        initialValues: { ...raw },
                    })
                })
                .forConfirm((payload, next) => {
                    next(payload)
                })
                .forCancel((payload, next) => {
                    next(payload)
                })
                .open()
                .then(async (values) => {
                    const info_arr = values['ru_form_info'];
                    if (info_arr.length === 0) return;
                    const records = [];
                    for (const form_info of info_arr) {
                        const attrs = split6Value(form_info);
                        records.push({
                            project_id: values.project_id,
                            project_owner_id: values.project_owner_id,
                            ru_form_id: attrs[0],
                            ru_form_name: attrs[1],
                            user_type: attrs[2],
                        });
                    }
                    if (records.length === 0) return;
                    await this._remove({ api: owner_form_api, where: { project_id: values.project_id, project_owner_id: values.project_owner_id } })
                    await this._saveBatch({ api: owner_form_api, records })

                })
                .catch(console.error)
        },
        handFormUser(row) {
            this.$router.push({
                name: 'eva-project-owner-form-user-setting', query: {
                    project_id: row.project_id,
                    project_owner_id: row.id,
                    group_type: this.group_type,
                }
            })
        },
        handRemove(row) {
            this.$confirm("确定要删除?", "系统提示").then(async () => {
                await this._remove({ api, where: { id: row.id } });
                await this.loadPager(1);
            }).catch(console.error);

        }
    },

    data() {
        return {
            tableData: {
                count: 0,
                offset: 10,
                page: 1,
                rows: [],
            }
        }
    }
}
</script>